package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.service.ChapterService;
import com.atguigu.eduservice.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author AIR
 * @since 2022-09-20
 */
@RestController
@RequestMapping("/eduservice/chapter")
//@CrossOrigin
public class ChapterController {

    @Autowired
    private ChapterService chapterService;

    //课程大纲列表，根据课程id进行查询
    @GetMapping("getChapterVideo/{courseID}")
    public R getChapterVideo(@PathVariable String courseID){
        List<ChapterVo> list = chapterService.getChapterVideoByCourseId(courseID);
//        System.out.println(list);
        return R.ok().data("allChapterVideo",list);
    }

    //添加章节
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter){
        chapterService.save(eduChapter);
        return R.ok();
    }

    //根据章节id查询
    @GetMapping("getChapter/{chapterId}")
    public R getChapter(@PathVariable String chapterId){
        EduChapter eduChapter = chapterService.getById(chapterId);
        return R.ok().data("chapter",eduChapter);
    }   
    //修改章节
    @PutMapping("updateChapter/{chapterId}")
    public R updateChapter(@PathVariable("chapterId") String chapterId,@RequestBody EduChapter eduChapter){
        System.out.println(eduChapter);
        eduChapter.setId(chapterId);
        boolean flag = chapterService.updateById(eduChapter);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //删除章节的方法
    @DeleteMapping("{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){
        Boolean flag = chapterService.deleteChapter(chapterId);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }


}
